package cn.com.pusai.base.login.action;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.com.pusai.common.action.BaseAction;
import cn.com.pusai.common.config.SysProperties;

public class LoginAction extends BaseAction implements SessionAware,
		ServletResponseAware {

	private Map session;
	HttpServletResponse response;
	private String account;
	private String password;
	private String verifyCode;

	public LoginAction() {
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	protected String checkLogin() throws Exception {
		// LoginObject loginObject = null;
		String errorInfo = null;
		if ("true".equals(SysProperties.MUST_LOGIN)) {
			String verifyCodeSession = (String) session.get("VERIFY_CODE");
			if (verifyCode == null || !verifyCode.equals(verifyCodeSession)) {
				errorInfo = "验证码错误";
				addInfo(errorInfo);
				System.out.println("验证码错误");
				return "failure";
			}
		}
		System.out.println("验证码正确");
		return "success";
		// if (account != null && password != null) {
		// Object object = loginService.checkLogin(account, password);
		// if (object instanceof LoginObject) {
		// loginObject = (LoginObject) object;
		// session.put("LOGIN_OBJECT", loginObject);
		// if (SysProperties.ADMIN_LOGIN.equals(loginObject.getLogin()))
		// session.put(SysProperties.PERSON_MODULE_LIST, moduleService
		// .findModules());
		// else
		// session.put(SysProperties.PERSON_MODULE_LIST,
		// permissionService.findModulesByPersonId(loginObject
		// .getPersonId()));
		// } else {
		// errorInfo = (String) object;
		// }
		// }
		// if (loginObject == null && errorInfo == null) {
		// errorInfo = "帐号或密码不正确";
		// addInfo(errorInfo);
		// return "failure";
		// }
		// if (errorInfo != null) {
		// addInfo(errorInfo);
		// return "failure";
		// } else {
		// return "success";
		// }
	}

	public String login() throws Exception {
		return checkLogin();
	}

}
